<?php

// @formatter:off
/**
 * @file WCActiveTagBlastTags.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCActiveTagBlastTags.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/activetags/WCActiveTagBlastTags.php $
 */
// @formatter:on

/**
 * @class WCActiveTagBlastTags
 */
class WCActiveTagBlastTags extends WCActiveTagHook {
	/**
	 * Class constructor
	 */
	public function __construct() {
		// @{
		$this->_canIt["add"]["CL"] = false;
		$this->_canIt["add"]["CX"] = true;
		$this->_canIt["add"]["DI"] = false;
		$this->_canIt["add"]["IM"] = true;
		$this->_canIt["add"]["TA"] = false;

		$this->_canIt["remove"]["CL"] = false;
		$this->_canIt["remove"]["DI"] = false;
		$this->_canIt["remove"]["TA"] = false;
		// @}

		parent::__construct();
	}
	//
	// Public methods.
	public function canBeAddedCX() {
		//
		// Setting default value to be returned.
		$out = true;
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcErrno;
		global $wcError;
		//
		// Trash tag can only be added by a ORGANIZER or more.
		if(!$wcProfile->granted(WC_PERM_COMIC_BLAST)) {
			$wcErrno = 403;
			$wcError = "Tag '".WC_ACTIVE_TAG_BLAST."' access denied";
			$out = false;
		}
		//
		// Returning checks result.
		return $out;
	}
	public function canBeAddedIM() {
		//
		// Setting default value to be returned.
		$out = true;
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcErrno;
		global $wcError;
		//
		// Trash tag can only be added by a ORGANIZER or more.
		if(!$wcProfile->granted(WC_PERM_IMAGE_BLAST)) {
			$wcErrno = 403;
			$wcError = "Tag '".WC_ACTIVE_TAG_BLAST."' access denied";
			$out = false;
		}
		//
		// Returning checks result.
		return $out;
	}
	public function canBeRemovedALL() {
		return $this->canBeRemovedCX() && $this->canBeRemovedIM();
	}
	public function canBeRemovedCX() {
		return $this->canBeAddedCX();
	}
	public function canBeRemovedIM() {
		return $this->canBeAddedIM();
	}
	public function hasCronTasks() {
		return true;
	}
	public function runCronTasks($spacer = "") {
		//
		// Setting default value to be returned.
		$retry = false;
		//
		// Tag shortcut.
		$tag = $this->tag();
		//
		// Checking tag status.
		if($tag && $tag->ok()) {
			if($tag->count > 0 || $tag->icount > 0) {
				//
				// Group shortcut.
				$group = $tag->group();
				$root = $group->rootDirectory();

				$holder = WCTagsHolder::I();

				foreach($tag->comicsFull() as $comic) {
					echo "{$spacer}Blasting comic '{$comic}':\n";
					echo "{$spacer}\tMoving directory '{$root}': ";
					if($comic->directory != $root->id) {
						$comic->setDirectory($root);
						echo WCColorText::ShellGreen("Done\n");
					} else {
						echo WCColorText::ShellYellow("Ignored").": Already there\n";
					}

					foreach($holder->tagsOfComic($comic) as $subTag) {
						echo "{$spacer}\tRemoving tag '{$subTag}': ";
						$subTag->removeComic($comic);
						echo WCColorText::ShellGreen("Done\n");
					}
				}
				foreach($tag->imagesFull() as $image) {
					echo "{$spacer}Blasting image '{$image}':\n";
					echo "{$spacer}\tMoving directory '{$root}': ";
					if($image->directory != $root->id) {
						$image->setDirectory($root);
						echo WCColorText::ShellGreen("Done\n");
					} else {
						echo WCColorText::ShellYellow("Ignored").": Already there\n";
					}

					foreach($holder->tagsOfImage($image) as $subTag) {
						echo "{$spacer}\tRemoving tag '{$subTag}': ";
						$subTag->removeImage($image);
						echo WCColorText::ShellGreen("Done\n");
					}
				}

				$tag->reload();
				$retry = true;
			}
		} else {
			echo $spacer.WCColorText::ShellRed("Failed").": Tag '{$tag->name}' seems to have issues.\n";
		}

		return $retry;
	}
}
